package 线程的状态;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-09-22
 * Time: 16:56
 */
public class ThreadDemo7 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //使用匿名callable实现类
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for(int i =0;i<100;++i){
                    sum += i;
                }
                return sum;
            }
        });
        Thread t = new Thread(futureTask);
        t.start();
        int res = futureTask.get();
        System.out.println(res);
    }
}
